package cn.jia.spring7.web.servlet;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.*;

/**
 * 初始化并注册自定义的servlet、filter和listenner
 * Created by jia on 2018/4/8.
 */
public class MyservletInitializer implements WebApplicationInitializer{
    //通过重写这个方法，我们可以注册自己的servlet
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 1.注册自定义的servlet
        ServletRegistration.Dynamic myservlet = servletContext.addServlet("myservlet", MyServlet.class);
        //设置servlet处理的路径
        myservlet.addMapping("/custom/**");
        // 2.注册过滤器
        FilterRegistration.Dynamic myFilter = servletContext.addFilter("myFilter", MyFilter.class);
        //添加filter的映射路径
        myFilter.addMappingForUrlPatterns(null,false,"/cusom/**");
        // 3.设置文件上传
        DispatcherServlet ds = new DispatcherServlet();
        ServletRegistration.Dynamic registration = servletContext.addServlet("appServlet", ds);
        registration.addMapping("/");
        registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads"));


    }
}
